import React from "react";
import { Route, Switch } from "react-router-dom";
import Login from "./pages/login";
import Dashboard from "./pages/dashboard";
import ChatRecord from "./pages/chatrecord";
import MessageList from "./pages/messagelist";
import { inject } from "mobx-react";
import { autorun } from "mobx";
import { message } from "antd";

@inject(({ commonState }) => ({ commonState }))
class App extends React.Component {
  componentWillMount() {
    const remote = window.electron.remote;
    const isAllowLogin = remote.getGlobal("isAllowLogin");
    const platform = remote.getGlobal("platform");
    console.log("platform-isAllowLogin", `${platform}-${isAllowLogin}`);
    console.log("node:", remote.process.versions.node);
    console.log("electron:", remote.process.versions.electron);
    console.log("chrome:", remote.process.versions.chrome);

    if (isAllowLogin) {
      const store = remote.getGlobal("store");
      const currentWinId = remote.getCurrentWindow().id;
      const winName = store.get(String(currentWinId));
      if (winName === "chatlistview") {
        window.location.hash = `/chatrecord`;
      } else if (winName === "newMessageView") {
        window.location.hash = `/messagelist`;
      } else {
        window.location.hash = `/dashboard`;
      }
    } else {
      window.location.hash = `/login`;
    }

    autorun(() => {
      let initStatus = this.props.commonState.initStatus;
      if (initStatus === "login") {
        message.success("已连接.");
      }
    });
  }
  render() {
    return (
      <div>
        <Switch>
          <Route path="/login" component={Login} />
          <Route path="/dashboard" component={Dashboard} />
          <Route path="/chatrecord" component={ChatRecord} />
          <Route path="/messagelist" component={MessageList} />
        </Switch>
      </div>
    );
  }
}

export default App;
